<?php
// +----------------------------------------------------------------------
// | Park 停车场
// +----------------------------------------------------------------------
// | Author: cart <xiaozhao755@gmail.com>
// +----------------------------------------------------------------------
// | Datetime: 2018-3-8 14:01:00
// +----------------------------------------------------------------------
// | Copyright: 微量科技
// +----------------------------------------------------------------------

namespace app\qianyu\controller;

use think\Validate;
use think\Log;
use think\Db;
use org\BaiduMap;
use think\Model;
use think\Loader;
use org\HMongodb;
use think\Exception;
use app\dv1\model\Shop;

class Park extends Init
{
    private $OrderModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->Park = Loader::model('Park');
        $this->Coupon = Loader::model('Coupon');
        $this->Order = Loader::model('Order');
    }
    public function getAddress(){
        $address = input('post.address');
            $baidu = new BaiduMap();
            $coordinate = $baidu::getCoordinateByName($address);
            if(is_array($coordinate)){
                if($coordinate['result']['location']['lng']){
                    $lng = $coordinate['result']['location']['lng'];
                }
                if($coordinate['result']['location']['lat']){
                    $lat = $coordinate['result']['location']['lat'];
                }
                if($lat && $lng){
                    $citycode = $baidu::getCodeByPoint($lng, $lat);   
                }
            return ['data' => $citycode, 'code' => 200, 'msg' => '操作完成', 'status' => true];
        }else{
            return ['data'=>'','code'=>400,'msg'=>'订单创建失败','status'=>false];
        }
    }
    /*
     * 显示用户订单列表
     * */
    public function ParkList()
    {
        $param = input('post.');
        $validate = [
            'lng' => 'require',
            'lat' => 'require'
        ];
        $result = $this->validate($param, $validate);
        if (true !== $result) {
            return ['data' => '', 'code' => 400, 'msg' => '参数不正确或者参数不全', 'status' => false];
        }
        $baidu = new BaiduMap();
        if($param['lat'] && $param['lng']){
            $citycode = $baidu::getCodeByPoint($param['lng'], $param['lat']);   
        }else{
            $citycode = '';
        }
        if($citycode){
            $list = $this->Park->getParkListData($citycode['citycode']);
        }else{
            $list = [];
        }
        if ($list) {
            return ['data' => $list, 'code' => 200, 'msg' => '查询成功', 'status' => true];
        } else {
            return ['data' => $list, 'code' => 400, 'msg' => '没有该类型订单', 'status' => false];
        }
    }

    public function parkDetail(){
        $param = input('post.');
        $validate = [
            'parkId' => 'require',
        ];
        $result = $this->validate($param, $validate);
        if (true !== $result) {
            return ['data' => '', 'code' => 400, 'msg' => '参数不正确或者参数不全', 'status' => false];
        }
        $data = $this->Park->getParkDetailData($param['parkId']);

        return ['data' => $data, 'code' => 200, 'msg' => '查询成功', 'status' => true];
    }

    public function ParkCouponList(){
        $param = input('post.');
        $validate = [
            'uid' => 'require',
            'price' => 'require',
        ];
        $result = $this->validate($param, $validate);
        if (true !== $result) {
            return ['data' => '', 'code' => 400, 'msg' => '参数不正确或者参数不全', 'status' => false];
        }
        $result = $this->Coupon->getParkCouponList($param['uid']);
        $result2 = $this->Coupon->getParkCouponListCoulmn($param['uid']);
        foreach ($result as $key => &$v) {
            $v['sign'] = 0;
            // if($v['bprice'] == $param['price']) {
            //     $v['sign'] = 1;
            //     continue;
            // }
            if($v['bprice'] <= $param['price']){
                $param['price'] = $param['price'] - $v['bprice'];
                $v['sign'] = 1;
                continue;
            }

        }

        // $price = $param['price'];
        // $dd = digui($result2,$price);
        // ksort($dd);
        // print_r($dd);
        // exit;

        // foreach ($result2 as $key => $value) {

        // }
        // $findSumTo100;

        return ['data' => $result, 'code' => 200, 'msg' => '查询成功', 'status' => true];
    }

//     public function  findSumTo100($a,$n){
//     for ($i = 0,$j = $n - 1; $i != $j; ) {
  
//         $sum = $a[$i] + $a[$j];  
//         if ($sum == 100) {  
//             print_r("%d %d\n",$a[$i],$a[$j]);
//             $i++;  
//             $j--;  
//         }  
//         else if ($sum < 100){  
//             $i++;  
//         }  
//         else if ($sum > 100){  
//             $j--;  
//         }  
//     }  
// }  

    public function parkOrder(){
        $param = input('post.');
        $validate = [
            'park_id' => 'require',
            'day' => 'require',
            'order_time' => 'require',
        ];
        $result = $this->validate($param, 'ParkValidate.add');
        if (true !== $result) {
            return ['data' => '', 'code' => 400, 'msg' => $result, 'status' => false];
        }
        $param['order_time'] = strtotime($param['order_time']);
        $param['create_time'] = time();
        Db::startTrans();
        try{
            $couponList = explode(',',$param['coupon_id']);
            $couponNumber = count($couponList);
            $param['cid'] =  $this->Coupon->getParkCidData($couponList);
            $result =  $this->Order->addParkOrder($param);
            $result2 =  $this->Coupon->getParkCouponListData($couponList);
            $saveParkCoupon =  $this->Coupon->SaveParkCouponStatus($couponList);
            if($param['order_price'] < $result2[0]['bprice'] && $result2[0]['bprice'] > 0 && $result && $saveParkCoupon){
                Db::commit();
            }else{
                throw new Exception("error"); 
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['data' => '', 'code' => 400, 'msg' => $e->getMessage(), 'status' => false];
        }
        if($result){
            return ['data' => $this->Order->id, 'code' => 200, 'msg' => '预约成功', 'status' => true];
        }else{
            return ['data' => '', 'code' => 400, 'msg' => '参数不正确或者参数不全', 'status' => false];
        }
    }
}